#!/bin/bash

# 设置默认命令
COMMAND=${1:-"dev"}

# 加载 nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# 检查是否安装了nvm
if [ ! -f "$HOME/.nvm/nvm.sh" ]; then
    echo "正在安装 nvm..."
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
    
    # 加载 nvm
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
fi

# 安装并使用 Node.js v18 LTS
echo "正在安装 Node.js v18..."
nvm install 18
nvm use 18

# 配置npm镜像
echo "配置 npm 镜像..."
npm config set registry https://registry.npmmirror.com
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
npm config set electron_builder_binaries_mirror https://npmmirror.com/mirrors/electron-builder-binaries/

# 检查包管理器
if ! command -v pnpm &> /dev/null; then
    echo "正在安装 pnpm..."
    npm install -g pnpm
fi

# 配置pnpm镜像
echo "配置 pnpm 镜像..."
pnpm config set registry https://registry.npmmirror.com
pnpm config set electron_mirror https://npmmirror.com/mirrors/electron/
pnpm config set electron_builder_binaries_mirror https://npmmirror.com/mirrors/electron-builder-binaries/

case $COMMAND in
    "env")
        # 只安装依赖，不清理环境
        echo "安装依赖..."
        export ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
        export ELECTRON_BUILDER_BINARIES_MIRROR="https://npmmirror.com/mirrors/electron-builder-binaries/"
        
        # 重命名配置文件
        echo "重命名配置文件..."
        [ -f "postcss.config.js" ] && mv postcss.config.js postcss.config.cjs
        [ -f "tailwind.config.js" ] && mv tailwind.config.js tailwind.config.cjs
        
        # 安装依赖
        if [ ! -d "node_modules" ]; then
            pnpm install
        fi
        ;;
        
    "serve")
        # 启动开发服务器（支持热重载）
        echo "启动开发服务器..."
        if [ ! -f ".env" ]; then
            echo "创建 .env 文件..."
            cat > .env << EOL
VITE_DEV_SERVER_URL=http://localhost:3000
ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/
ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/
EOL
        fi

        # 创建必要的目录
        mkdir -p dist/main
        mkdir -p dist/renderer

        # 使用 nodemon 监视后端文件变化
        if [ ! -d "node_modules/nodemon" ]; then
            pnpm add -D nodemon
        fi

        # 启动开发服务器和后端服务
        if [ -f "node_modules/.bin/concurrently" ]; then
            node_modules/.bin/concurrently \
                "VITE_DEV_SERVER_URL=http://localhost:3000 node_modules/.bin/vite" \
                "node_modules/.bin/nodemon --watch src/main -e ts,js --exec electron ."
        else
            pnpm add -D concurrently
            node_modules/.bin/concurrently \
                "VITE_DEV_SERVER_URL=http://localhost:3000 node_modules/.bin/vite" \
                "node_modules/.bin/nodemon --watch src/main -e ts,js --exec electron ."
        fi
        ;;
        
    "dev")
        # 清理环境并重新安装
        echo "清理环境..."
        rm -rf node_modules
        rm -rf dist
        rm -rf dist-electron
        rm -f pnpm-lock.yaml
        pnpm store prune

        # 重命名配置文件
        echo "重命名配置文件..."
        [ -f "postcss.config.js" ] && mv postcss.config.js postcss.config.cjs
        [ -f "tailwind.config.js" ] && mv tailwind.config.js tailwind.config.cjs

        # 安装依赖
        echo "正在安装项目依赖..."
        export ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
        export ELECTRON_BUILDER_BINARIES_MIRROR="https://npmmirror.com/mirrors/electron-builder-binaries/"
        pnpm install

        # 检查是否需要创建环境变量文件
        if [ ! -f ".env" ]; then
            echo "创建 .env 文件..."
            cat > .env << EOL
VITE_DEV_SERVER_URL=http://localhost:3000
ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/
ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_DATABASE=pdf_tool
EOL
        fi

        # 创建必要的目录
        echo "创建必要的目录..."
        mkdir -p dist/main
        mkdir -p dist/renderer

        # 运行开发服务器
        echo "启动开发服务器..."
        if [ -f "node_modules/.bin/vite" ]; then
            VITE_DEV_SERVER_URL=http://localhost:3000 node_modules/.bin/vite
        else
            echo "错误: vite 未找到，请确保依赖安装正确"
            exit 1
        fi
        ;;
        
    *)
        echo "未知命令: $COMMAND"
        echo "可用命令: env, serve, dev"
        exit 1
        ;;
esac 